package com.wondersgroup.fjzlpt.console.dao.impl.yljgxx;

import java.util.*;

import com.quick.framework.boot.repository.sql.jdbcTemplate.impl.TableBeanBaseJTDaoImpl;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Repository;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.quick.framework.boot.repository.sql.bean.SQLBean;
import com.quick.framework.boot.repository.sql.bean.page.PageQuerySQLBean;
import com.quick.framework.boot.repository.sql.bean.table.TableBean;
import com.quick.framework.boot.repository.sql.jdbcTemplate.impl.BaseJTDAOImpl;
import com.quick.framework.boot.repository.sql.jdbcTemplate.rs.ResultHandlerConstant;
import com.wondersgroup.fjzlpt.console.dao.inter.yljgxx.YljgxxDAO;

@Repository
public class YljgxxDAOImpl extends BaseJTDAOImpl implements YljgxxDAO{

	/**
	 * 机构信息
	 * @param tb
	 */
	@Override
	@Autowired
	@Qualifier("TB_ZD_YLJGXX")
	public void setTb(TableBean tb) {
		super.setTb(tb);
	}
	
	public JSONArray queryAll()
	{
		StringBuilder sbud = new StringBuilder("select * from view_yljgxx");
		SQLBean sql = new SQLBean(sbud.toString());
		return super.queryList(sql);
	}

	/**
	 * 查询概要信息
	 * @return
	 */
	public JSONObject queryPageSummary(Map<String, String> map){
		StringBuilder sbud = new StringBuilder("select PTJGID, YLJGMC, YLJGDM, XZQHDM, XZQHMC, WSJGLBMC, YYDJDM, YYDJMC from view_yljgxx where 1=1 \n");
		List<String> list = new ArrayList<>(2);
		String key = map.remove("YLJGMC");
		if(!StringUtils.isEmpty(key)){
			sbud.append(" and YLJGMC like ?");
			list.add("%"+key+"%");
		}
		key = map.remove("JGXZQHDM");
		if(!StringUtils.isEmpty(key)){
			sbud.append(" and XZQHDM like ?");
			list.add("%"+key+"%");
		}
		key = map.remove("PTJGID");
		if(!StringUtils.isEmpty(key)){
			sbud.append(" and PTJGID = ?");
			list.add(key);
		}

		PageQuerySQLBean sb = checkAndCreatePageQuerySQLBean(map, sbud.toString(), "order by XZQHDM, YLJGDM", list.toArray());
		return super.queryPage(sb, ResultHandlerConstant.JSONARRAY);
	}

	@Override
	public JSONObject queryPage(Map<String, String> map) {
		JSONObject sqlObj = this.getPageSql(map);
		List list = (List)sqlObj.get("sqlParams");
		
		PageQuerySQLBean sb = checkAndCreatePageQuerySQLBean(map, sqlObj.getString("sql"), "order by a.XZQHDM, a.YLJGDM", list.toArray());
		return super.queryPage(sb, ResultHandlerConstant.JSONARRAY);
	}
	
	/**
	 * 根据条件查询所有数据
	 * @param map
	 * @return
	 */
	@Override
	public JSONArray queryAll(Map map){
		JSONObject sqlObj = this.getPageSql(map);
		
		String sql = sqlObj.getString("sql") + " order by a.XZQHDM, a.YLJGDM";
		List list = (List)sqlObj.get("sqlParams");
		
		
		SQLBean bean = new SQLBean(sql, list.toArray());
		return super.queryList(bean);
	}
	
	
	/**
	 * 合成分页sql
	 * @param map
	 * @return
	 */
	private JSONObject getPageSql(Map<String, String> map){
		JSONObject result = new JSONObject();
		
		StringBuilder sbud = new StringBuilder("select a.* from view_yljgxx a where 1=1 \n");
		List list = new ArrayList();
		String value = map.get("PTJGID");
		if(!StringUtils.isEmpty(value))
		{
			sbud.append("and a.PTJGID = ?");
			list.add(value);
		}else{
			value = map.get("YLJGMC");
			if(!StringUtils.isEmpty(value))
			{
				sbud.append("and (a.YLJGMC like ? or a.JGJC like ? or a.YLJGMCQP like ? or a.YLJGMCJP like ?)");
				list.add("%"+value+"%");
				list.add("%"+value+"%");
				list.add("%"+value.toUpperCase()+"%");
				list.add("%"+value.toUpperCase()+"%");
			}
			value = map.get("WSJGLBDM");
			if(!StringUtils.isEmpty(value))
			{
				sbud.append("and a.WSJGLBDM like ?");
				list.add("%"+value+"%");
			}
			value = map.get("JGXZQHDM");
			if(!StringUtils.isEmpty(value))
			{
				sbud.append("and a.XZQHDM like ?");
				list.add(value+"%");
			}
		}
		
		result.put("sql", sbud.toString());
		result.put("sqlParams", list);
		return result;
	}

	/**
	 * 判断记录是否存在
	 * @return
	 */
	public boolean isExist(Map<String, String> map) {
		String YLJGDM = map.get("YLJGDM");
		StringBuilder sbud = new StringBuilder("select count(1) from TB_ZD_YLJGXX where YLJGDM=?");
		SQLBean sb = new SQLBean(sbud.toString(), new Object[]{YLJGDM});
		return this.isExist(sb);
	}
}
